package com.hong.service_cart.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hong.fy_model.model.cart.CartEntity;
import com.hong.service_cart.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 
 *
 * @author wqh
 * @email 1601709391@qq.com
 * @date 2022-06-16 21:42:56
 */
@RestController
@RequestMapping("/api/cart")
@Api(value = "Feign调用接口", tags = "前端不调用")
public class CartApiController {

    @Autowired
    private CartService cartService;

    @ApiOperation(value = "新增购物车")
    @PostMapping("/addCart/{userId}")
    public CartEntity add(@PathVariable("userId") @ApiParam("用户ID") Integer userId){
        CartEntity cart = new CartEntity();
        cart.setUserId(userId); //设置用户ID
        //根据用户ID查询购物车，如果没有则新增，如果有则不新增，一个用户只能有一个购物车
        LambdaQueryWrapper<CartEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CartEntity::getUserId, userId);
        CartEntity cartEntity = cartService.getOne(queryWrapper);
//        System.out.println("Feigin调用成功"+cartEntity);
        if (cartEntity == null){
            cartService.save(cart);
            return cart;
        }else {
            return null;
        }


    }


}
